pacman::p_load(tidyverse, data.table, sf, raster)
rm(list=ls())
################################################################################

#Location of raw shapefiles
shpfolder_list <- c("../../data/shapefiles/raw/arg_adm_unhcr2017_shp",
                    "../../data/shapefiles/raw/bra_adm_ibge_2020_shp",
                    "../../data/shapefiles/raw/World_Countries__Generalized_-shp")

#Standardize variable names
for (shpfolder in shpfolder_list){
  
  if(shpfolder=="../../data/shapefiles/raw/arg_adm_unhcr2017_shp") {
    
    region = 'argentina'
    level = 'county'
    shpfile_name = "arg_admbnda_adm2_unhcr2017"
    old_cols = c("ADM0_ES","ADM0_PCODE","ADM1_ES","ADM1_PCODE","ADM2_ES","ADM2_REF","ADM2_PCODE","geometry")
    new_cols = c("ctry","ctry_id","st","st_id","cty_or","cty","cty_id","geometry")
    final_cols = c("ctry","st","st_id","cty","cty_id","geometry")
    
  } else {
    
    if(shpfolder=="../../data/shapefiles/raw/bra_adm_ibge_2020_shp") {
      
      region = 'brazil'
      level = 'county'
      shpfile_name = "bra_admbnda_adm2_ibge_2020"
      old_cols = c("ADM0_EN","ADM0_PT","ADM0_PCODE","ADM1_PT","ADM1_PCODE","ADM2_PT","ADM2_PCODE","ET_ID","geometry")
      new_cols = c("ctry","ctry_or","ctry_id","st","st_id","cty","cty_id","et_id","geometry")      
      final_cols = c("ctry","st","st_id","cty","cty_id","geometry")
      
    } else {
      
      region = 'world'
      level = 'country'
      shpfile_name = "World_Countries__Generalized_"
      old_cols = c("FID","COUNTRY","ISO","COUNTRYAFF","AFF_ISO","SHAPE_Leng","SHAPE_Area","geometry")
      new_cols = c("fid","ctry","iso","ctry_aff","iso_aff","shape_leng","shape_area","geometry")
      final_cols = c("fid","ctry","iso","ctry_aff","iso_aff","geometry")
      
    }
  }
  
  if(!dir.exists(shpfolder)) {unzip( paste0(shpfolder,".zip"), exdir = shpfolder)}
  
  shpfile = sf::read_sf(dsn=path.expand(shpfolder), layer=shpfile_name)
  names(shpfile) <- new_cols
  shpfile = shpfile[,final_cols]
  shpfile <- st_zm(shpfile, drop=T, what='ZM')
  if(region=='argentina'){shpfile_ref_crs = shpfile}
  if(region=='brazil'){shpfile = shpfile %>% st_set_crs(st_crs(shpfile_ref_crs))}
  st_write(shpfile, dsn = "../../data/shapefiles/clean/", layer = paste(region,level,sep='_'), driver = "ESRI Shapefile", append=FALSE, quiet=TRUE)
  unlink(shpfolder, recursive=TRUE)
  
}

#Create combined Argentina + Brazil shapefile
shpfile_ar = sf::read_sf(dsn=path.expand("../../data/shapefiles/clean/"), layer='argentina_county')
shpfile_br = sf::read_sf(dsn=path.expand("../../data/shapefiles/clean/"), layer='brazil_county')
shpfile_arbr = rbind(shpfile_ar,shpfile_br)
st_write(shpfile_arbr, dsn = "../../data/shapefiles/clean/", layer='argentinabrazil_county', driver = "ESRI Shapefile", append=FALSE, quiet=TRUE)

#Create rest of South America shapefile
shpfile = sf::read_sf(dsn=path.expand("../../data/shapefiles/clean/"), layer='world_country')
shpfile <- shpfile[shpfile$ctry %in% c('Chile', 'Uruguay','Bolivia', 'Paraguay','Peru','Ecuador','Colombia','Venezuela','Guyana','French Guiana','Suriname'), ]
shpfile$st = shpfile$ctry
shpfile$st_id = shpfile$ctry
shpfile$cty = shpfile$ctry
shpfile$cty_id = shpfile$ctry
final_cols = c("ctry","st","st_id","cty","cty_id")
shpfile = shpfile[,final_cols]
shpfile_rosa <- st_crop(shpfile, extent(-85,-32, -65, 11.75)) #bound to mainland South America
st_write(shpfile_rosa, dsn = "../../data/shapefiles/clean/",  layer='restofsouthamerica_country', driver = "ESRI Shapefile", append=FALSE, quiet=TRUE)

#Create combined South America shapefile
shpfile_sa = rbind(shpfile_arbr,shpfile_rosa)
st_write(shpfile_sa, dsn = "../../data/shapefiles/clean/", layer='southamerica_county', driver = "ESRI Shapefile", append=FALSE, quiet=TRUE)




